package org.example.business;

import org.example.model.OrderItem;
import org.example.model.ShoppingCart;
import org.example.ui.milk_order.MilkTeaItemPanel;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddToCartBusiness {
    public static JPanel jPanel;
    public static JLabel numberLabel;
    public static Map<String, Integer> map = new HashMap<>();
    public static Map<String, MilkTeaItemPanel> itemMap = new HashMap<>();
    public static ShoppingCart cart = new ShoppingCart();
    public static void setOrderItems() {
        jPanel.removeAll();
        List<MilkTeaItemPanel> orderLists = new ArrayList<>();
        jPanel.setPreferredSize(new Dimension(150, 70 * cart.orderItemList.size()));
        SpringLayout jPanelLayout = new SpringLayout();
        jPanel.setLayout(jPanelLayout);
        if(cart.orderItemList.isEmpty()) {
            numberLabel.setText("");
            jPanel.setVisible(false);
            jPanel.setVisible(true);
            return;
        }
        cart.calculateTotal();
        for (int i = 0; i < cart.orderItemList.size(); i++){
            MilkTeaItemPanel orderItemPanel = new MilkTeaItemPanel(cart.orderItemList.get(i));
            orderLists.add(orderItemPanel);
            jPanel.add(orderItemPanel);
            if (i == 0) {
                jPanelLayout.putConstraint(SpringLayout.NORTH, orderLists.getFirst(),1, SpringLayout.NORTH, jPanel);
                jPanelLayout.putConstraint(SpringLayout.SOUTH, orderLists.getFirst(),70, SpringLayout.NORTH, jPanel);
            }
            else {
                jPanelLayout.putConstraint(SpringLayout.NORTH, orderLists.get(i),1, SpringLayout.SOUTH, orderLists.get(i-1));
                jPanelLayout.putConstraint(SpringLayout.SOUTH, orderLists.get(i),70, SpringLayout.SOUTH, orderLists.get(i-1));
            }
            jPanelLayout.putConstraint(SpringLayout.WEST, orderLists.get(i),1, SpringLayout.WEST, jPanel);
            jPanelLayout.putConstraint(SpringLayout.EAST, orderLists.get(i),-1, SpringLayout.EAST, jPanel);
        }
        numberLabel.setText("" + cart.getTotal());
        jPanel.setVisible(false);
        jPanel.setVisible(true);
    }
}
